angular.module('starter')
  .config(function($stateProvider) {
    $stateProvider.state('index', {
      url: '/index',
      templateUrl: 'templates/index/index.html',
      controller: 'indexCtrl',
      controllerAs: 'indexCtrl',
      onEnter: function($rootScope) {
        $rootScope.$broadcast('onEnterIndex');
      },
    })
  })
  .controller('indexCtrl', function($scope, $rootScope, $http) {

    // 百度api调取天气预报
    $http
      .jsonp('http://api.map.baidu.com/telematics/v3/weather?location=%E4%B8%B9%E9%98%B3&output=json&callback=JSON_CALLBACK&ak=ryGGzhrZxutxxgj6q4TPjUGq')
      .success(function(data) {
        if(data.status == 'success' && data.results && data.results.length) {
          this.weatherInfo = data.results[0].weather_data[0];
        }
      }.bind(this));

    this.statisticInfo = {
      draftsNum: 0,
      messageNum: 0
    };

    this.getStatisticInfo = function() {
      $http.post("qss/statistic/statisticOtherNum").success(function(res) {
        if (res.success) {
          this.statisticInfo = res.rows;
        }
      });
    };

    this.getStatisticInfo();

    $rootScope.$on("onEnterIndex", function() {
      this.getStatisticInfo();
    });

  });